package io.github.talelin.latticy.service.K;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.dto.K.BExceptionDTO;
import io.github.talelin.latticy.mapper.K.BExceptionMapper;
import io.github.talelin.latticy.model.K.BExceptionDO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.latticy.model.K.BExceptionDetailDO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 状态：0、未确认；1、已确认 服务实现类
 * </p>
 * @author generator@TaleLin
 * @since 2021-06-08
 */
@Service
public class BExceptionService extends ServiceImpl<BExceptionMapper, BExceptionDO> {

    @Autowired
    private BExceptionMapper BExceptionMapper;

    public BExceptionDO getById(Integer id) {
        BExceptionDO b_gateway = BExceptionMapper.selectById(id);
        return b_gateway;
    }

    //垃圾代码
    public IPage<BExceptionDO> seachBException(Page<BExceptionDO> pager, String keyword,String StartTime,String EndTime,String dwmc,String bgateway_name,String collector_name,String state,String operation,
                                               String dwmc1,
                                               String bgateway_name1,
                                               String collector_name1,
                                               String operation1,
                                               String dwmc2,
                                               String bgateway_name2,
                                               String collector_name2,
                                               String operation2,
                                               String dwmc3,
                                               String bgateway_name3,
                                               String collector_name3,
                                               String operation3,
                                               String dwmc4,
                                               String bgateway_name4,
                                               String collector_name4,
                                               String operation4){
        QueryWrapper<BExceptionDO> wrapper = new QueryWrapper<>();
        if(StartTime != ""){
            wrapper.ge("collector_time",StartTime);
        }
        if(EndTime != ""){
            wrapper.le("collector_time",EndTime);
        }
//        if(dwmc != ""){
//            wrapper.eq("dwmc",dwmc);
//        };
//        if(bgateway_name != ""){
//            wrapper.eq("b_gateway_name",bgateway_name);
//        };
//        if(collector_name != ""){
//            wrapper.eq("collector_name",collector_name);
//        };
        if(state != ""){
            wrapper.eq("state",state);
        };
//        if(operation != ""){
//            wrapper.eq("operation",operation);
//        };
//        if(dwmc1 != ""){
//            wrapper.or().eq("dwmc",dwmc1);
//        };
//        if(bgateway_name1 != ""){
//            wrapper.or().eq("b_gateway_name",bgateway_name1);
//        };
//        if(collector_name1 != ""){
//            wrapper.or().eq("collector_name",collector_name1);
//        };
//        if(operation1 != ""){
//            wrapper.or().eq("operation",operation1);
//        };
//        if(dwmc2 != ""){
//            wrapper.or().eq("dwmc",dwmc2);
//        };
//        if(bgateway_name2 != ""){
//            wrapper.or().eq("b_gateway_name",bgateway_name2);
//        };
//        if(collector_name2 != ""){
//            wrapper.or().eq("collector_name",collector_name2);
//        };
//        if(operation2 != ""){
//            wrapper.or().eq("operation",operation2);
//        };
//        if(dwmc3 != ""){
//            wrapper.or().eq("dwmc",dwmc3);
//        };
//        if(bgateway_name3 != ""){
//            wrapper.or().eq("b_gateway_name",bgateway_name3);
//        };
//        if(collector_name3 != ""){
//            wrapper.or().eq("collector_name",collector_name3);
//        };
//        if(operation3 != ""){
//            wrapper.or().eq("operation",operation3);
//        };
//        if(dwmc4 != ""){
//            wrapper.or().eq("dwmc",dwmc4);
//        };
//        if(bgateway_name4 != ""){
//            wrapper.or().eq("b_gateway_name",bgateway_name4);
//        };
//        if(collector_name4 != ""){
//            wrapper.or().eq("collector_name",collector_name4);
//        };
//        if(operation4 != ""){
//            wrapper.or().eq("operation",operation4);
//        };
        if(dwmc != "") {
            wrapper.and(i -> i.eq("dwmc", dwmc)
                    .or().eq("dwmc", dwmc1)
                    .or().eq("dwmc", dwmc2)
                    .or().eq("dwmc", dwmc3)
                    .or().eq("dwmc", dwmc4));
        }
        if(bgateway_name != "") {
            wrapper.and(i -> i.eq("b_gateway_name", bgateway_name)
                    .or().eq("b_gateway_name", bgateway_name1)
                    .or().eq("b_gateway_name", bgateway_name2)
                    .or().eq("b_gateway_name", bgateway_name3)
                    .or().eq("b_gateway_name", bgateway_name4));
        }
        if(collector_name != "") {
            wrapper.and(i -> i.eq("collector_name", collector_name)
                    .or().eq("collector_name", collector_name1)
                    .or().eq("collector_name", collector_name2)
                    .or().eq("collector_name", collector_name3)
                    .or().eq("collector_name", collector_name4));
        }
        if(operation != "") {
            wrapper.and(i -> i.eq("operation", operation)
                    .or().eq("operation", operation1)
                    .or().eq("operation", operation2)
                    .or().eq("operation", operation3)
                    .or().eq("operation", operation4));
        }
        wrapper.and(i -> i.like("operation",keyword)
                .or().like("dwmc",keyword)
                .or().like("collector_name",keyword)
                .or().like("b_gateway_name",keyword));
        IPage<BExceptionDO> iPage = BExceptionMapper.selectPage(pager,wrapper);
        return iPage;
    }

    public BExceptionDetailDO getDetail(Long id){

        return this.getBaseMapper().getDetail(id);
    }

    public List<BExceptionDetailDO> getBException(){

        return this.getBaseMapper().getBException();
    }

    public void AllConfirmed(){

        this.getBaseMapper().AllConfirmed();
    }

    public BExceptionDO confirm(Integer id){

        return this.getBaseMapper().confirm(id);
    }

    public void update(BExceptionDTO dto, Long id) {
        BExceptionDO BExceptionDO = this.getById(id);
        if (BExceptionDO == null) {
            throw new NotFoundException(70000);
        }
        BeanUtils.copyProperties(dto, BExceptionDO);
        this.updateById(BExceptionDO);
    }
}
